import { FormProps, FormSchema } from '/@/components/Table';

import { BasicColumn } from '/@/components/Table/src/types/table';
import { getJulyDictItemListByTypeApi } from '@/api/sys/julyDictItemApi';
import { h } from 'vue';
import Icon from '@/components/Icon/Icon.vue';
import {getAliVideoInfoApi} from "@/api/resource/lessonApi";
import {message} from "ant-design-vue";
let statusJson: JulyDictItemListGetResultModel =[];
const getStatusJson = async () => {
  getJulyDictItemListByTypeApi({ type: 'STATUS' }).then((res)=>{
    statusJson=res;
  })
};
  getStatusJson();
//表格显示列
export function getBasicColumns(): BasicColumn[] {
  // @ts-ignore
  return [
    {
      title: '课时名称',
      dataIndex: 'title',
      width: 260,
    },
    {
      title: '序列',
      dataIndex: 'indexOrder',
      width: 150,
    },

    {
      title: '课时阿里云id',
      dataIndex: 'videoPath',
      width: 150,
    },
    {
      title: '课时时长',
      dataIndex: 'medialength',
      width: 150,
    },
    {
      title: '课程',
      dataIndex: 'courseName',
      width: 150,
    },
    {
      title: '状态',
      dataIndex: 'status',
      width: 150,
      customRender: ({ record }) => {
        let statusName = '正常';
        if (record.status && statusJson && statusJson.length > 0) {
          const status = statusJson.filter((item) => item.value == record.status);
          if (status) {
            statusName = status.length > 0 ? status[0].label : '正常';
          }
        }
        return statusName;
      },
    },
    {
      title: '授课老师',
      dataIndex: 'author',
      width: 150,
    },
  ];
}

export function getFormConfig(): Partial<FormProps> {
  return {
    labelWidth: 150,
    baseColProps: { lg: 6, md: 24 },
    schemas: formParamSchema,
  };
}

//查询条件表单
export const formParamSchema: FormSchema[] = [
  /*  {
      field: 'name',
      label: '租户名称',
      component: 'Input',
    },*/
];

//编辑表单
export const formSchema: FormSchema[] = [
  {
    field: 'title',
    label: '课时名称',
    component: 'Input',
    required: false,
  },
  {
    field: 'status',
    label: '状态',
    component: 'ApiSelect',
    required: true,
    componentProps: {
      // more details see /src/components/Form/src/components/ApiSelect.vue
      api: getJulyDictItemListByTypeApi,
      params: {
        type: 'STATUS',
      },
      // use name as label
      labelField: 'label',
      // use id as value
      valueField: 'value',
      // not request untill to select
      immediate: true,
    },
  },
  {
    field: 'author',
    label: '授课老师',
    component: 'Input',
    required: false,
  },
  {
    field: 'content',
    label: '授课内容',
    component: 'Input',
    required: false,
  },
  {
    field: 'indexOrder',
    label: '序列',
    component: 'InputNumber',
    required: true,
  },

  {
    field: 'videoPath',
    label: '课时阿里云id',
    component: 'Input',
    required: false,
    componentProps: ({ formModel }) => {
      return {
        onBlur: (e: ChangeEvent) => {
          getAliVideoInfoApi(e.target.value).then(res=>{
            console.log(res);
            formModel.medialength=res.video.duration*1000;
          }).catch(error=> {
            message.error("视频id错误，请查正");
          });

        },
      };
    },
  },
  {
    field: 'medialength',
    label: '课时时长',
    component: 'Input',
    required: true,
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'parentCourseId',
    label: '课程',
    component: 'Input',
    required: false,
    show: false,
  },
];
